package com.hcl.queue;

import java.util.Scanner;

/**
 * 数组模拟队列
 */
public class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue queue = new ArrayQueue(3);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while(loop) {
            System.out.println("s(show): 查看队列");
            System.out.println("e(exit): 退出");
            System.out.println("a(add): 添加元素");
            System.out.println("g(get): 获取元素");
            System.out.println("h(head): 查看头部元素");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("添加元素");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int res = queue.getQueue();
                        System.out.printf("获取元素%d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = queue.headQueue();
                        System.out.printf("头部元素%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("结束");
    }

}
class ArrayQueue  {
    private int maxSize;    // 队列最大容量
    private int front;      // 指向队列头部元素（不包含）
    private int rear;       // 指向队列尾部元素
    private int[] arr;

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.front = -1;
        this.rear = -1;
        arr = new int[maxSize];
    }
    public boolean isFull(){
        return rear == maxSize-1;
    }

    // 当front == rear为空
    public boolean isEmpty(){
        return front == rear;
    }

    /**
     * 添加元素到队列
     * @param n
     */
    public void addQueue(int n){
        if (isFull()){
            System.out.println("队列满");
            return;
        }
        // 添加元素 头部指针不变 尾部指针增加
        rear++;
        arr[rear] = n;
    }

    public int getQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列空");
        }
        // 获取元素 头部指针增加 尾部指针不变
        front++;
        return arr[front];
    }

    public void showQueue(){
        if (isEmpty()){
            System.out.println("队列空");
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("arr[%d]=%d\n", i, arr[i]);
        }
    }

    public int headQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列空");
        }
        return arr[front+1];
    }
}
